package com.zy.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zy.entity.Position;
import com.zy.entity.StaffBranchDp;
import com.zy.mapper.PositionMapper;
import com.zy.mapper.StaffBranchDpMapper;
import com.zy.service.PositionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PositionServiceImpl implements PositionService {

    @Resource
    PositionMapper positionMapper;

    @Resource
    StaffBranchDpMapper staffBranchDpMapper;

    @Override
    public Position getById(Integer id) {
        return (Position)this.positionMapper.selectByPrimaryKey(id);
    }

    @Override
    public void save(Position position) {
        this.positionMapper.insert(position);
    }

    @Override
    public void updateById(Position position) {
        this.positionMapper.updateByPrimaryKey(position);
    }

    @Override
    public PageInfo<Position> page(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        return new PageInfo(this.positionMapper.selectAll());
    }

    @Override
    public void delete(Integer id) {
        this.positionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void status(Integer id) {
        Position position = positionMapper.selectByPrimaryKey(id);
        position.setStatus(position.getStatus()==0?1:0);
        this.positionMapper.updateByPrimaryKey(position);
    }

    @Override
    public List<Position> getByStaffId(Integer staffId) {
        return  positionMapper.getByStaffId(staffId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void setByStaffId(Integer staffId, List<Integer> roleIds) {
        StaffBranchDp staffBranchDp = new StaffBranchDp();
        staffBranchDp.setSfId(staffId);
        staffBranchDpMapper.delete(staffBranchDp);
        for (Integer roleId : roleIds) {
            StaffBranchDp staffBranchDp1 = new StaffBranchDp();
            staffBranchDp1.setSfId(staffId);
            staffBranchDp1.setRid(roleId);
            staffBranchDpMapper.insert(staffBranchDp1);
        }
    }

    @Override
    public List<Position> all() {
        return  positionMapper.selectAll();
    }


}
